package com.doradance.app.curriculum;


import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.donkingliang.labels.LabelsView;
import com.doradance.R;
import com.doradance.account.UrlCenter;
import com.doradance.account.utils.ShareUtil;
import com.doradance.app.appactivity.search.SearchActivity;
import com.doradance.app.appactivity.search.SearchCurriumActivity;
import com.doradance.entity.CurriculumEntity;
import com.doradance.entity.SelectCurriteEntity;
import com.ogaclejapan.smarttablayout.SmartTabLayout;
import com.ogaclejapan.smarttablayout.utils.v4.Bundler;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItemAdapter;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItems;
import com.pachong.android.baseuicomponent.fragment.BaseFragment;
import com.pachong.android.framework.httprequest.HttpRequest;
import com.pachong.android.framework.httprequest.responsecallbacks.DataRequestListener;
import com.pachong.android.frameworkbase.utils.EasyToast;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.doradance.app.curriculum.CurriculumListFragment.KEY_CHANNELID;

/**
 * A simple {@link Fragment} subclass.
 */
public class CurriculumFragment extends BaseFragment {


    @Bind(R.id.vptab_home)
    SmartTabLayout mVptabHome;
    @Bind(R.id.vp_home)
    ViewPager mVpHome;
    @Bind(R.id.tv_curriculum_hot)
    TextView mTvCurriculumHot;
    @Bind(R.id.tv_curriculum_new)
    TextView mTvCurriculumNew;
    @Bind(R.id.dl_curril)
    DrawerLayout mDlCurril;
    @Bind(R.id.labels_vip_grade)
    LabelsView mLabelsVipGrade;
    @Bind(R.id.labels_vip_age)
    LabelsView mLabelsVipAge;
    @Bind(R.id.labels_vip_diffcult)
    LabelsView mLabelsVipDiffcult;
    String vipId ="";
    String ageId ="";
    String difficultLevel ="";

    public CurriculumFragment() {
        setRetainInstance(true);  //转屏不需要重新创建
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        mDlCurril.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
        ShareUtil.setIsHot(true);
        ShareUtil.setIsNew(false);
        mTvCurriculumHot.setTextColor(Color.parseColor("#1a191d"));
        getChanelList();
        return rootView;
    }


    //获得频道数据
    private void getChanelList() {
        HttpRequest request = new HttpRequest(getContext());
        request.get(UrlCenter.COURSES_INDEX, null, new DataRequestListener<CurriculumEntity>(CurriculumEntity.class) {
            @Override
            public void onStart(Context context, String url) {
                super.onStart(context, url);
            }
            @Override
            public void onSuccess(CurriculumEntity o) {
                super.onSuccess(o);
                List<CurriculumEntity.CategoriesBean> categories = o.getCategories();
                initLabels(o);
                initData(categories);

            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
                EasyToast.showToast(getContext(), strMsg);
            }
        });
    }

    private void initLabels(CurriculumEntity o) {
        //会员等级
        mLabelsVipGrade.setSelectType(LabelsView.SelectType.SINGLE);
        final ArrayList<CurriculumEntity.VipsBean> vipLabelsEntities = new ArrayList<>();
        for (int i = 0; i < o.getVips().size(); i++) {
            vipLabelsEntities.add(o.getVips().get(i));
        }
        mLabelsVipGrade.setLabels(vipLabelsEntities, new LabelsView.LabelTextProvider<CurriculumEntity.VipsBean>() {
            @Override
            public CharSequence getLabelText(TextView label, int position, CurriculumEntity.VipsBean data) {
                return data.getName();
            }
        });
        mLabelsVipGrade.setOnLabelClickListener(new LabelsView.OnLabelClickListener() {
            @Override
            public void onLabelClick(TextView label, Object data, int position) {
                CurriculumEntity.VipsBean data1 = (CurriculumEntity.VipsBean) data;
                vipId = data1.getId() + "";
            }
        });
        //年龄段
        mLabelsVipAge.setSelectType(LabelsView.SelectType.SINGLE);
        final ArrayList<CurriculumEntity.AgeRangesBean> vipAgeEntities = new ArrayList<>();
        for (int i = 0; i < o.getAge_ranges().size(); i++) {
            vipAgeEntities.add(o.getAge_ranges().get(i));
        }
        mLabelsVipAge.setLabels(vipAgeEntities, new LabelsView.LabelTextProvider<CurriculumEntity.AgeRangesBean>() {
            @Override
            public CharSequence getLabelText(TextView label, int position, CurriculumEntity.AgeRangesBean data) {
                return data.getName();
            }
        });
        mLabelsVipAge.setOnLabelClickListener(new LabelsView.OnLabelClickListener() {
            @Override
            public void onLabelClick(TextView label, Object data, int position) {
                CurriculumEntity.AgeRangesBean  data1 = (CurriculumEntity.AgeRangesBean) data;
                ageId=data1.getId()+"";
            }
        });

        //难度
        mLabelsVipDiffcult.setSelectType(LabelsView.SelectType.SINGLE);
        final ArrayList<CurriculumEntity.DifficultiesBean> vipDiffcultEntities = new ArrayList<>();
        for (int i = 0; i < o.getDifficulties().size(); i++) {
            vipDiffcultEntities.add(o.getDifficulties().get(i));
        }
        mLabelsVipDiffcult.setLabels(vipDiffcultEntities, new LabelsView.LabelTextProvider<CurriculumEntity.DifficultiesBean>() {
            @Override
            public CharSequence getLabelText(TextView label, int position, CurriculumEntity.DifficultiesBean data) {
                return data.getName();
            }
        });
        mLabelsVipDiffcult.setOnLabelClickListener(new LabelsView.OnLabelClickListener() {
            @Override
            public void onLabelClick(TextView label, Object data, int position) {
                CurriculumEntity.DifficultiesBean data1 = (CurriculumEntity.DifficultiesBean) data;
                difficultLevel=data1.getLevel()+"";
            }
        });

    }

    private void initData(List<CurriculumEntity.CategoriesBean> categories) {
        //遍历添加
        FragmentPagerItems.Creator items = FragmentPagerItems.with(getContext());
        for (CurriculumEntity.CategoriesBean channelsEntity : categories) {
            if (channelsEntity.getId() != -1) {
                items.add(channelsEntity.getName(), CurriculumListFragment.class,
                        new Bundler().putInt(KEY_CHANNELID, channelsEntity.getId()).get());
            }
        }
        FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(getChildFragmentManager(), items.create());
        mVpHome.setAdapter(adapter);
        mVpHome.setOffscreenPageLimit(categories.size());
        mVptabHome.setViewPager(mVpHome);
//        mVptabHome.addOnTa
    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_curriculum, container, false);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.tv_curriculum_hot, R.id.tv_curriculum_new, R.id.ll_select,R.id.rl_comfirm,R.id.iv_search_currium})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_curriculum_hot:
                if (!ShareUtil.getIsHot()) {
                    EventBus.getDefault().post(new SelectCurriteEntity("learning_count desc", "", "", ""));
                    mTvCurriculumHot.setTextColor(Color.parseColor("#1a191d"));
                    mTvCurriculumNew.setTextColor(Color.parseColor("#B0B0B0"));
                }
                break;
            case R.id.tv_curriculum_new:
                if (!ShareUtil.getIsNew()) {
                    EventBus.getDefault().post(new SelectCurriteEntity("create_time desc", "", "", ""));
                    mTvCurriculumNew.setTextColor(Color.parseColor("#1a191d"));
                    mTvCurriculumHot.setTextColor(Color.parseColor("#B0B0B0"));
                }
                break;
            case R.id.ll_select:
                mDlCurril.openDrawer(Gravity.RIGHT);
                break;

            case R.id.rl_comfirm:
                if(TextUtils.isEmpty(ageId)&&TextUtils.isEmpty(vipId)&&TextUtils.isEmpty(difficultLevel)){
                    EasyToast.showToast(getContext(),"请至少选择一种条件");
                }else{
                    ShareUtil.setVipId(vipId);
                    ShareUtil.setAgeId(ageId);
                    ShareUtil.setDifficult(difficultLevel);
                    if(!ShareUtil.getIsNew()){
                        mDlCurril.closeDrawer(Gravity.RIGHT);
                        EventBus.getDefault().post(new SelectCurriteEntity("create_time desc", ShareUtil.getVipId(), ShareUtil.getAgeId(), ShareUtil.getDifficult()));
                    }else{
                        mDlCurril.closeDrawer(Gravity.RIGHT);
                        EventBus.getDefault().post(new SelectCurriteEntity("learning_count desc", ShareUtil.getVipId(), ShareUtil.getAgeId(), ShareUtil.getDifficult()));
                    }
                }
                break;

            case R.id.iv_search_currium:
                SearchCurriumActivity.start(getActivity());
                break;
        }
    }
}
